<?php

class Ext_Avatar_Show_Adapter_Fastdfs extends Ext_Avatar_Show_Abstract
{
    public function __construct()
    {
    }
    
    public function build($imageId)
    {
        $image = App_Model_DbTable_Avatar::getInstance()->getRowById($imageId);
        if(!$image){
            return "";
        }

        $this->big    = $this->_buildUrl($image['big']);
        $this->middle = $this->_buildUrl($image['middle']);
        $this->small  = $this->_buildUrl($image['small']);
        $this->thumb  = $this->_buildUrl($image['thumb']);
    }
    
    private function _buildUrl($imageFastdfsId)
    {
        if(!$imageFastdfsId){
            return "";
        }
        $imageFastdfs = App_Model_DbTable_Image_Fastdfs::getInstance()->getRowById($imageFastdfsId);
        if(!$imageFastdfs){
            return "";
        }
        $group  = $imageFastdfs['group'];
        $fileId = $imageFastdfs['fileId'];
        
        $config = Ext_Config::getInstance()->getConfig('fastdfs');
        $url = '';
        if(isset($config->group->$group->scheme)){
            $url .= $config->group->$group->scheme.'://';
        }
        if(isset($config->group->$group->host)){
            $url .= $config->group->$group->host;
        }
        if($config->group->$group->port != '80'){
            $url .= ':'.$config->group->$group->port;
        }
        
        $url .= "/".$fileId;
        
        return $url;
    }
}
